﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace SqlExecuter.Client
{
    class SettingsManager
    {
        private const string settingsFilename = "Settings.xml";
        public static string[] GetRecentLocations()
        {
            XDocument doc = XDocument.Load(settingsFilename);

            var recentDirectories = from dir in doc.Descendants("RecentLocations").Descendants("RecentLocation")
                                    select dir.Value;
            return recentDirectories.ToArray();
        }

        public static void UpdateRecentLocations(IEnumerable<string> recentPaths)
        {
            if (recentPaths.Count() > 0)
            {
                try
                {
                    XDocument doc = XDocument.Load(settingsFilename);
                    doc.Descendants("RecentLocations").Descendants().Remove();
                    var x = doc.Descendants("RecentLocations").FirstOrDefault();
                    foreach (string s in recentPaths)
                    {
                        x.Add(new XElement("RecentLocation", s));
                    }
                    doc.Save(settingsFilename);
                }
                catch (Exception ex)
                {
                    // Consume the exception in case there is one, during the file save.
                }
            }

        }
    }
}
